package com.tcoiss.datafactory.controller;


import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.datafactory.domain.MqTopic;
import com.tcoiss.datafactory.service.IMqTopicService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 消息主题发布Controller
 *
 * @author zw
 * @date 2021-05-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/topic" )
public class MqTopicController extends BaseController {

    private final IMqTopicService iMqTopicService;

    /**
     * 查询消息主题发布列表
     */
    @PreAuthorize(hasPermi = "mq:topic:list")
    @GetMapping("/list")
    public TableDataInfo list(MqTopic mqTopic) {
        startPage();
        List<MqTopic> list = iMqTopicService.queryList(mqTopic);
        return getDataTable(list);
    }

    /**
     * 导出消息主题发布列表
     */
    @PreAuthorize(hasPermi = "mq:topic:export" )
    @Log(title = "消息主题发布" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,MqTopic mqTopic) throws IOException
    {
        List<MqTopic> list = iMqTopicService.queryList(mqTopic);
        ExcelUtil<MqTopic> util = new ExcelUtil<MqTopic>(MqTopic.class);
        util.exportExcel(response,list, "topic" );
    }

    /**
     * 获取消息主题发布详细信息
     */
    @PreAuthorize(hasPermi = "mq:topic:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iMqTopicService.getById(id));
    }

    /**
     * 同步消息主题
     */
    @PreAuthorize(hasPermi = "bus:table:syncTopic")
    @Log(title = "消息主题发布", businessType = BusinessType.SYNC)
    @PostMapping("/syncTopic")
    public AjaxResult syncTableJg() {
        return toAjax(iMqTopicService.syncKdTopic() ? 1 : 0);
    }

    /**
     * 新增消息主题发布
     */
    @PreAuthorize(hasPermi = "mq:topic:add" )
    @Log(title = "消息主题发布" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MqTopic mqTopic) {
        return toAjax(iMqTopicService.save(mqTopic) ? 1 : 0);
    }

    /**
     * 修改消息主题发布
     */
    @PreAuthorize(hasPermi = "mq:topic:edit" )
    @Log(title = "消息主题发布" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MqTopic mqTopic) {
        return toAjax(iMqTopicService.updateById(mqTopic) ? 1 : 0);
    }

    /**
     * 删除消息主题发布
     */
    @PreAuthorize(hasPermi = "mq:topic:remove" )
    @Log(title = "消息主题发布" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iMqTopicService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     *  更新使用状态
     */
    @PreAuthorize(hasPermi = "mq:topic:edit")
    @GetMapping(value = "/updateStatus")
    public AjaxResult updateStatus(Long[] workIds, Integer status){
        return AjaxResult.success(iMqTopicService.updateUseStatus(Arrays.asList(workIds),status) ? 1 : 0);
    }
}
